package base.datastructure.stack;

import java.util.Deque;
import java.util.LinkedList;

/**
 * 题解：采用Deque双端队列接口
 * 继承自Queue接口，Deque的实现类是LinkedList、ArrayDeque、LinkedBlockingDeque
 *
 * 普通队列(一端进另一端出):
 *      Queue queue = new LinkedList()或Deque deque = new LinkedList()
 * 双端队列(两端都可进出)
 *      Deque deque = new LinkedList()
 * 堆栈
 *      Deque deque = new LinkedList()
 */
public class MinStack {
    //一个栈用来存放历次的最小值
    Deque<Integer> min;
    //一个栈用来存放内容
    Deque<Integer> minStack;
    public MinStack() {
        min = new LinkedList<>();
        minStack = new LinkedList<>();
    }

    public void push(int val) {
        minStack.push(val);
        if (min.peek() == null){
            min.push(val);
        }else {
            min.push(Math.min(min.peek(),val));
        }
    }

    public void pop() {
        minStack.pop();
        min.pop();
    }

    public int top() {
        return minStack.peek();
    }

    public int getMin() {
        return min.peek();
    }
}
